package com.probiz.estore.imports.handler.catalog.productCategory;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;

import com.probiz.estore.catalog.service.ProductCategoryManager;
import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;

public class ParentCategorySimplePathNameHandler extends ColumnBasicHandler implements ColumnHandler{
	private Logger logger = Logger.getLogger(ParentCategorySimplePathNameHandler.class);
	protected ProductCategoryManager	productCategoryManager	= null;
	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public void setProperty(ImportModel importModel, Column column)throws Exception {
		ProductCategory category=(ProductCategory)importModel.getTarget();
		
		ProductCategory parentCategory=null;
		if(StringUtils.isNotEmpty(column.getValue())){
			//case：有指定父目录简单路径的，就搜索获得
			parentCategory=getCategoryByPathName(importModel,column.getValues().toArray(new String[]{}));
			if(category.getParent()!=null && parentCategory!=null  && !category.getParent().getCategoryId().equals(parentCategory.getCategoryId())){
				//当为更新时，如果上级目录发生变更。
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "本条数据为更新，原上级目录不能进行变更！，请将【上级目录完整路径】留空或者填写正确的信息。";
				logger.error(msg);
				importModel.setMsg(msg);
				return;
			} else if( parentCategory == null){
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "无法找到所对应的  【上级目录完整路径】，无法导入";
				logger.error(msg);
				importModel.setMsg(msg);
				return;
			}
		} else {
			//如果为空则在根目录下
			parentCategory=productCategoryManager.getById(ProductCategory.ROOT_ID);
		}
		category.setParent(parentCategory);
		importModel.setResult(ImportModel.RESULT_SUCCESS);
	}
	
	
	public ProductCategory getCategoryByPathName(ImportModel importModel,String categoryNames[]){
		Integer parentCategoryId=ProductCategory.ROOT_ID;
		ProductCategory parentCategory=productCategoryManager.getById(ProductCategory.ROOT_ID);
		for (String categoryName : categoryNames) {
			List<ProductCategory> categoryList=productCategoryManager.listCategoryByParentIdAndName(parentCategoryId, categoryName);
			if(0 == categoryList.size()){
				//设置结果
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "在目录【"+parentCategory.getCategoryName()+"】下找不到目录名称为【"+categoryName+"】的目录，无法导入";
				logger.error(msg);
				importModel.setMsg(msg);
				return null;
			}else if(1 == categoryList.size()){	
				parentCategory = categoryList.get(0);
				parentCategoryId = parentCategory.getCategoryId();
			}else{
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "在目录【"+parentCategory.getCategoryName()+"】下找到多个目录名称均为【"+categoryName+"】的目录，系统无法识别";
				logger.error(msg);
				importModel.setMsg(msg);
				return null;
			}
		}
		return parentCategory;
	}

	
	

}
